<template>
  <div>
    <MyTable :goodsList="list">
      <template #head>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <!-- 作用域插槽，然后解构 -->
      <template #tbody="{ row }">
        <td>{{ row.id }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>
          <!-- input -->
          <input
          class="tag-input form-control"
          style="width: 100px;"
          type="text"
          v-if="row.inputVisible"
          @blur="row.inputVisible=false"
          v-model="row.inputValue"
          @keydown.esc="row.inputValue=''"
          @keydown.enter="enterFn(row)"
          v-fff
          />
          <button 
          v-else 
          style="display: block;" 
          class="btn btn-primary btn-sm add-tag"
          @click="row.inputVisible = true"
          >+Tag</button>
          <span
            class="badge badge-warning"
            v-for="(item, index) in row.tags"
            :key="index"
          >
            {{ item }}
          </span>
        </td>
        <td>
          <!-- 点击删除，传入所点项的id -->
          <button class="btn btn-danger btn-sm" @click="deleteFn(row.id)">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable";

export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    };
  },
  created() {
    // 请求数据
    this.$axios({
      url: "/api/goods",
      method: "get",
    }).then((res) => {
      this.list = res.data.data;
    });
  },
  methods: {
    // 删除点击的对应项
    deleteFn(id) {
     this.list= this.list.filter(item=>item.id!==id)
    },
    // input输入
    enterFn(row){
      if(row.inputValue.trim()==='')return alert('输入不合法')
      row.tags.push(row.inputValue)
      row.inputValue=''
    }
  },
  directives:{
    fff:{
      inserted(el){
        el.focus()
      }
    }
  }
};
</script>
<style>

</style>